package cn.edu.jsu.zjj.service.impl;

import cn.edu.jsu.zjj.constant.CommonConstant;
import cn.edu.jsu.zjj.exception.BaseException;
import cn.edu.jsu.zjj.mapper.DocuMapper;
import cn.edu.jsu.zjj.model.dto.docu.DocuQueryRequest;
import cn.edu.jsu.zjj.model.entity.Docu;
import cn.edu.jsu.zjj.model.utils.SqlUtils;
import cn.edu.jsu.zjj.service.DocuService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class DocuServiceImpl extends ServiceImpl<DocuMapper, Docu> implements DocuService {

    @Autowired
    private DocuMapper docuMapper;

    @Override
    public QueryWrapper<Docu> getQueryWrapper(DocuQueryRequest docuQueryRequest) {
        if (docuQueryRequest == null) {
            throw new BaseException("请求参数为空");
        }

        String name = docuQueryRequest.getName();
        String status = docuQueryRequest.getStatus();
        String operName = docuQueryRequest.getOperName();
        String sortField = docuQueryRequest.getSortField();
        String sortOrder = docuQueryRequest.getSortOrder();
        QueryWrapper<Docu> queryWrapper = new QueryWrapper<>();
        // 添加去掉软删除条件
        queryWrapper.ne("isDelete", 1);
        queryWrapper.like(StringUtils.isNotBlank(name), "name", name);
        queryWrapper.like(StringUtils.isNotBlank(status), "status", status);
        queryWrapper.like(StringUtils.isNotBlank(operName), "operName", operName);
        queryWrapper.orderBy(SqlUtils.validSortField(sortField), sortOrder.equals(CommonConstant.SORT_ORDER_ASC), sortField);
        return queryWrapper;
    }

    @Override
    public QueryWrapper<Docu> getMyQueryWrapper(DocuQueryRequest docuQueryRequest) {
        if (docuQueryRequest == null) {
            throw new BaseException("请求参数为空");
        }
        LocalDateTime startTime = docuQueryRequest.getCreateTime();
        LocalDateTime endTime = docuQueryRequest.getEndTime();
        String name = docuQueryRequest.getName();
        String operName = docuQueryRequest.getOperName();
        String status = docuQueryRequest.getStatus();
        String sortField = docuQueryRequest.getSortField();
        String sortOrder = docuQueryRequest.getSortOrder();
        QueryWrapper<Docu> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(name), "name", name);
        queryWrapper.like(StringUtils.isNotBlank(status), "status", status);
        queryWrapper.like(StringUtils.isNotBlank(operName), "operName", operName);
        queryWrapper.between(startTime != null && endTime != null, "createTime", startTime, endTime);
        queryWrapper.orderBy(SqlUtils.validSortField(sortField), sortOrder.equals(CommonConstant.SORT_ORDER_ASC), sortField);
        return queryWrapper;
    }

    @Override
    public Page<Docu> queryDocu(Page<Docu> page, String name, String userMajorId) {
        LambdaQueryWrapper<Docu> queryWrapper = new LambdaQueryWrapper<>();

        if (name != null && !name.isEmpty()) {
            queryWrapper.like(Docu::getName, name);
        }

        if (userMajorId != null && !userMajorId.isEmpty()) {
            queryWrapper.and(wrapper -> {
                wrapper.like(Docu::getMajorId, userMajorId)
                        .or().like(Docu::getMajorId, userMajorId + "-");
            });
        }

        queryWrapper.eq(Docu::getIsDelete, 0);
        queryWrapper.orderByDesc(Docu::getCreateTime);

        return this.page(page, queryWrapper);
    }
}